package Servlet;

import Dao.checkDao;
import Dao.check_recordDao;
import bean.ReservationObject;
import bean.check;
import bean.check_record;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@WebServlet(urlPatterns = "/checkQueueServlet")
public class checkQueueServlet extends BaseServlet{

    checkDao checkDao = new checkDao();
    check_recordDao dao = new check_recordDao();

    //获取正在排队的检查队列，并存入session域中
    protected void checkQueue(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<check_record> recordList = dao.queryRecordBYStatus("Pending");//获取正在排队的患者检查记录列表
        recordList.sort(Comparator.comparing(check_record::getCreated_time));//对获取的患者通过创建时间进行排队
        int i = 0;//代表当前记录的序号
        for (check_record checkRecord : recordList) {
//            System.out.println(checkRecord);
            String value = String.valueOf(i);
            checkRecord.setSerialnumber(value);
            i++;//自增加1
        }
        HttpSession session = req.getSession();
        session.setAttribute("checkRecordQueue",recordList);//将排队队列存储到session域中
        req.getRequestDispatcher("/checkQueueServlet?action=queueCheckInformation").forward(req,resp);
    }


    protected void queueCheckInformation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
       //获取正在排队的检查信息记录
        List<check_record> queue = (List<check_record>) session.getAttribute("checkRecordQueue");
        //获取预约检查的病人信息
        ReservationObject patientInformation = (ReservationObject) session.getAttribute("patientInformation");
        //找出身份ID相同的检查预约信息
        List<check_record> PatientCheckRecords = new ArrayList<>();
        for (check_record record : queue) {
            if(patientInformation.getIdNumber().equals(record.getIdNumber())){
                check checkByName = checkDao.queryCheckByName(record.getExamination_name());
                record.setCheck(checkByName);//
                PatientCheckRecords.add(record);
            }
        }

        for (check_record record : PatientCheckRecords) {
            System.out.println(record);
        }

        session.setAttribute("PatientCheckRecords",PatientCheckRecords);
        req.getRequestDispatcher("/ReservationSuccessPage.jsp").forward(req,resp);
    }

}
